//
//  FriendProfileViewController.m
//  iTweet
//
//  Created by Dang Duc Nam (namk42c@yahoo.com) on 5/12/09.
//  Copyright 2009 Unitech. All rights reserved.
//


#import "FriendProfileViewController.h"
#import "FriendProfileView.h"
#import "FriendProfileFlippedView.h"
#import "User.h"
#import "WebViewController.h"
#import "iTweetAppDelegate.h"
#import "UserTimelineViewController.h"
@implementation FriendProfileViewController
@synthesize user;
@synthesize friendProfileFlippedView;
@synthesize friendProfileView;
@synthesize containerView;
@synthesize reflectionView;
@synthesize flipIndicatorButton;
@synthesize frontViewIsVisible;

#define reflectionFraction 0.35
#define reflectionOpacity 0.5

- (id)init {
	if (self = [super init]) {
		user = nil;
		friendProfileView = nil;
		friendProfileFlippedView = nil;
		self.frontViewIsVisible=YES;
//		self.hidesBottomBarWhenPushed = YES;
		appDelegate = (iTweetAppDelegate *)[[UIApplication sharedApplication] delegate];
	}
	return self;
}

// The designated initializer. Override to perform setup that is required before the view is loaded.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
*/
- (void)loadView {	
	// create and store a container view
	
	UIView *localContainerView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	self.containerView = localContainerView;
	[localContainerView release];
	
	containerView.backgroundColor = [UIColor blackColor];
	
	CGSize preferredViewSize = [FriendProfileView preferredViewSize];
	
	CGRect viewRect = CGRectMake((containerView.bounds.size.width-preferredViewSize.width)/2,
								 (containerView.bounds.size.height-preferredViewSize.height)/2-40,
								 preferredViewSize.width,preferredViewSize.height);
	
	// create the atomic element view
	FriendProfileView *localView = [[FriendProfileView alloc] initWithFrame:viewRect];
	self.friendProfileView = localView;
	[localView release];
	
	// add the atomic element view to the containerView
	friendProfileView.friendProfile = user;	
	[containerView addSubview:friendProfileView];
	
	friendProfileView.viewController = self;
	self.view = containerView;
	
	// create the atomic element flipped view
	
	FriendProfileFlippedView *localFlippedView = [[FriendProfileFlippedView alloc] initWithFrame:viewRect];
	self.friendProfileFlippedView = localFlippedView;
	[localFlippedView release];
	
	friendProfileFlippedView.friendProfile = user;	
	friendProfileFlippedView.viewController = self;
	
	
	// create the reflection view
	CGRect reflectionRect=viewRect;
	
	// the reflection is a fraction of the size of the view being reflected
	reflectionRect.size.height=reflectionRect.size.height*reflectionFraction;
	
	// and is offset to be at the bottom of the view being reflected
	reflectionRect=CGRectOffset(reflectionRect,0,viewRect.size.height);
	
	UIImageView *localReflectionImageView = [[UIImageView alloc] initWithFrame:reflectionRect];
	self.reflectionView = localReflectionImageView;
	[localReflectionImageView release];
	
	// determine the size of the reflection to create
	NSUInteger reflectionHeight=friendProfileView.bounds.size.height*reflectionFraction;
	
	// create the reflection image, assign it to the UIImageView and add the image view to the containerView
	reflectionView.image=[self.friendProfileView reflectedImageRepresentationWithHeight:reflectionHeight];
	reflectionView.alpha=reflectionOpacity;
	
	[containerView addSubview:reflectionView];
	
	
	UIButton *localFlipIndicator=[[UIButton alloc] initWithFrame:CGRectMake(0,0,30,30)];
	self.flipIndicatorButton=localFlipIndicator;
	[localFlipIndicator release];
	
	// front view is always visible at first
	[flipIndicatorButton setBackgroundImage:[UIImage imageNamed:@"flipper_list_blue.png"] forState:UIControlStateNormal];
	
	UIBarButtonItem *flipButtonBarItem;
	flipButtonBarItem=[[UIBarButtonItem alloc] initWithCustomView:flipIndicatorButton];
	
	[self.navigationItem setRightBarButtonItem:flipButtonBarItem animated:YES];
	[flipButtonBarItem release];
	
	[flipIndicatorButton addTarget:self action:@selector(flipCurrentView) forControlEvents:(UIControlEventTouchDown   )];
	
	
}

- (void)flipCurrentView {
	NSUInteger reflectionHeight;
	UIImage *reflectedImage;
	
	// disable user interaction during the flip
	containerView.userInteractionEnabled = NO;
	flipIndicatorButton.userInteractionEnabled = NO;
	
	// setup the animation group
	[UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(transitionDidStop:finished:context:)];
	
	// swap the views and transition
    if (frontViewIsVisible==YES) {
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:containerView cache:YES];
        [friendProfileView removeFromSuperview];
        [containerView addSubview:friendProfileFlippedView];
		
		
		// update the reflection image for the new view
		reflectionHeight=friendProfileFlippedView.bounds.size.height*reflectionFraction;
		reflectedImage = [friendProfileFlippedView reflectedImageRepresentationWithHeight:reflectionHeight];
		reflectionView.image=reflectedImage;
    } else {
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:YES];
        [friendProfileFlippedView removeFromSuperview];
        [containerView addSubview:friendProfileView];
		// update the reflection image for the new view
		reflectionHeight=friendProfileView.bounds.size.height*reflectionFraction;
		reflectedImage = [friendProfileView reflectedImageRepresentationWithHeight:reflectionHeight];
		reflectionView.image=reflectedImage;
    }
	[UIView commitAnimations];
	
	
	[UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(transitionDidStop:finished:context:)];
	
	UIImage *avatarImage = [appDelegate getUserNormalImage:user];
	avatarImage = [ImageManipulation makeRoundCornerImage:avatarImage : 4 : 4];
	
	if (frontViewIsVisible==YES)
	{
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:flipIndicatorButton cache:YES];
		[flipIndicatorButton setBackgroundImage:[user flipperImageForFriendProfileNavigationItem:avatarImage] forState:UIControlStateNormal];
	}
	else
	{
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:flipIndicatorButton cache:YES];
		[flipIndicatorButton setBackgroundImage:[UIImage imageNamed:@"flipper_list_blue.png"] forState:UIControlStateNormal];
		
	}
	[UIView commitAnimations];
	frontViewIsVisible=!frontViewIsVisible;
}


- (void)transitionDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
	// re-enable user interaction when the flip is completed.
	containerView.userInteractionEnabled = YES;
	flipIndicatorButton.userInteractionEnabled = YES;
	
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (void)jumpToTwitter:(id)sender {
	// create the string that points to the correct Wikipedia page for the element name
	if(webViewer == nil)
		webViewer = [[WebViewController alloc] initWithNibName:@"WebView" bundle:[NSBundle mainBundle]];
	 
	webViewer.screen_name = user.screen_name;
	 
	[self.navigationController pushViewController:webViewer animated:YES];
	
}

- (void)jumpToStatus:(id)sender {
	if(statusViewer == nil)
		statusViewer = [[UserTimelineViewController alloc] initWithNibName:@"UserTimeLineView" bundle:[NSBundle mainBundle]];
	
	statusViewer.username = [user.ident stringValue];
	
	[self.navigationController pushViewController:statusViewer animated:YES];
}

- (void)doUnFollow:(id)sender {
	//containerView.userInteractionEnabled = NO;
	//[appDelegate performSelectorOnMainThread:@selector(disableUpdatesFor:) withObject:[user.ident stringValue] waitUntilDone:YES];
	[appDelegate disableUpdatesFor:[user.ident stringValue]];
	//containerView.userInteractionEnabled = YES;
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc {
	[friendProfileView release];
	[reflectionView release];
	[friendProfileFlippedView release];
	[user release];
	[super dealloc];
}


@end
